﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Zhp.Api.Attributes;
using Zhp.Auth;
using Zhp.IService.Sys;
using Zhp.Model.Common;
using Zhp.Model.Sys;

namespace Zhp.Api.Controllers.Sys
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiGroup(GroupName = ApiGroupEnum.Sys)]
    public class LoginController : ControllerBase
    {
        ILoginService loginSvc;
        public LoginController(ILoginService loginSvc)
        {
            this.loginSvc = loginSvc;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ObjectRet<LoginRet>> Login(LoginParam p)
        {
            var res = await loginSvc.Login(p);
            return res;
        }


        /// <summary>
        /// 刷新登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowOnline]
        [Authorize(AuthenticationSchemes = RefreshTokenDefaults.AuthenticationScheme)]
        public async Task<ObjectRet<TokenRet>> RefreshLogin()
        {
            var res = await loginSvc.RefreshLogin();
            return res;
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<bool> Logout()
        {
            return await loginSvc.Logout();
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ObjectRet<LoginRet>> Register(LoginParam p)
        {
            return await loginSvc.Login(p);
        }
    }
}
